package com.zx._12_算法.左程云算法练习;

/**
 * 动态数组。实现add 和扩容
 */
public class Case_06 {

    private int[] data;
    private int size;


    public Case_06() {
        this.data = new int[4];
        this.size = 0;
    }

    public static void main(String[] args) {
        Case_06 case_06 = new Case_06();
        for (int i = 0; i < 100; i++) {
            case_06.add(i);
        }
        case_06.foreach();
    }

    public void add(int num) {
        System.out.println("add num=" + num);
        if (this.size + 1 >= data.length) {
            this.kuorong();
        }
        this.data[size++] = num;
    }

    public void foreach() {
        for (int i = 0; i < size; i++) {
            System.out.print(data[i] + ",");
        }
        System.out.println();
    }

    private void kuorong() {
        System.out.println("kuorong");
        int[] res = new int[data.length * 2];
        for (int i = 0; i < data.length; i++) {
            res[i] = data[i];
        }
        this.data = res;
    }


}
